import os, shutil
import cPickle as pickle

_save_dir = os.path.join('data', 'save', 'hero')
_game_path = os.path.join(_save_dir, 'game.save')

def save_game(game):
    try:
        os.makedirs(_save_dir)
    except OSError:
        pass
    file_ = open(_game_path, 'wb')
    pickle.dump(game, file_, pickle.HIGHEST_PROTOCOL)

def load_game():
    file_ = open(_game_path, 'rb')
    return pickle.load(file_)

def remove_save_dir():
    shutil.rmtree(_save_dir)

def save_level(level):
    path = _level_path(level.level_num)
    try:
        os.makedirs(_save_dir)
    except OSError:
        pass
    file_ = open(path, 'wb')
    pickle.dump(level, file_, pickle.HIGHEST_PROTOCOL)

def load_level(level_num):
    path = _level_path(level_num)
    file_ = open(path, 'rb')
    return pickle.load(file_)

def _level_path(level_num):
    return os.path.join(_save_dir, 'level_%d.save' % level_num)
